# 清理UE项目的IntelliSense缓存和临时文件
Write-Host "开始清理Unreal项目的IntelliSense缓存..."

# 项目路径
$ProjectPath = "g:\data\Game Engine\EmptyLand"

# 创建临时目录存储备份
$BackupDir = Join-Path $env:TEMP "UEIntelliSenseBackup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
New-Item -ItemType Directory -Path $BackupDir -Force | Out-Null

# 清理以下文件类型
$FileTypesToClean = @(
    "*.sdf",      # 智能感知数据库
    "*.suo",      # 解决方案用户选项
    "*.opensdf",  # 临时智能感知数据库
    "*.VC.db",    # Visual C++ 数据库
    "*.vs",       # Visual Studio 缓存目录
    "*.sln",      # 解决方案文件（如果需要重新生成）
    "Binaries",   # 二进制文件目录
    "Intermediate", # 中间文件目录
    "DerivedDataCache" # 派生数据缓存
)

# 执行清理
foreach ($FileType in $FileTypesToClean) {
    if ($FileType -like "*.*") {
        # 清理文件
        $Files = Get-ChildItem -Path $ProjectPath -Filter $FileType -Recurse -ErrorAction SilentlyContinue
        foreach ($File in $Files) {
            Write-Host "备份文件: $($File.FullName)"
            $BackupPath = Join-Path $BackupDir (Split-Path $File.FullName -Leaf)
            Copy-Item -Path $File.FullName -Destination $BackupPath -Force
            Remove-Item -Path $File.FullName -Force
            Write-Host "已删除: $($File.FullName)"
        }
    } else {
        # 清理目录
        $Dirs = Get-ChildItem -Path $ProjectPath -Directory -Filter $FileType -Recurse -ErrorAction SilentlyContinue
        foreach ($Dir in $Dirs) {
            Write-Host "备份目录: $($Dir.FullName)"
            $BackupPath = Join-Path $BackupDir (Split-Path $Dir.FullName -Leaf)
            Copy-Item -Path $Dir.FullName -Destination $BackupPath -Recurse -Force
            Remove-Item -Path $Dir.FullName -Recurse -Force
            Write-Host "已删除: $($Dir.FullName)"
        }
    }
}

Write-Host "IntelliSense缓存清理完成！备份保存在: $BackupDir"
Write-Host "建议下一步操作:"
Write-Host "1. 重新打开Visual Studio"
Write-Host "2. 或通过Unreal Editor重新生成项目文件"
Write-Host "3. 或直接打开EmptyLand.uproject文件并在编辑器中构建项目"